<?php
namespace app\api\controller;

use app\api\common\controller\Api;
use hg\apidoc\annotation as Apidoc;
use think\facade\Config;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use app\admin\model\meal\Package as PackageModel;
use app\admin\model\meal\PackageAttr as PackageAttrModel;
use app\admin\model\management\Lease as LeaseModel;
/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Package extends Api
{

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];
    public function __construct(){
        $this->PackageModel=new PackageModel();
        $this->PackageAttrModel=new PackageAttrModel();
        $this->LeaseModel=new LeaseModel();
    }
    public function getPackageLists(){
        $where=[
            ['status','=',1]
        ];
        $page=input('page');
        $size=input('size');
        $list=$this->PackageModel->field('id,title,price,activity_price,thumb')->where($where)->page($page,$size)->select();
    
        $this->result('success',$list,200,'json');
    }
    public function getPackageDetials(){
         $data=input("get.");
        $rule = [
            'id|id参数'   => 'require|number',
        ];
        $result =$this->validate($data, $rule);
        $where['id']=$data['id'];
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $res =$this->PackageModel->where($where)->find();
        if($res->main_image) $res['main_image']=explode('|',$res->main_image);
        if($res->detail_image) $res['detail_image']=explode('|',$res->detail_image);
        $attr=$this->PackageAttrModel->where('package_id',$data['id'])->select();
        foreach ($attr as $k => $v) {
            $attr[$k]['server_name']=$this->LeaseModel->where('id',$v->server_id)->value('title');
        }
        $res['server']=$attr;
         $this->result('success',$res,200,'json');
    }
    
}